# bin/bash
echo "--------------------- bash start ----------------"

# 设置程序名
if [ ! -n "$APP_NAME" ]; then
    # 程序名 为空 则退出
    echo "!!!!!!!!!!!!!! APP_NAME not found !!!!!!!!!!!!"
    exit 0
fi

# JVM启动参数设置
JAVA_OPTS=""

# 程序环境
EP=""

# 设置应用环境，默认为dev环境
if [ -n "$APP_ENV" ]; then
    EP="--spring.profiles.active=${APP_ENV}"
else
    EP="--spring.profiles.active=dev"
fi

# 设置程序IP
if [ -n "$APP_IP" ]; then
    # 设置Nacos注册应用IP
    EP=${EP}" --spring.cloud.nacos.discovery.ip=${APP_IP}"
fi

# 设置 VM size 默认600m
JVM_P='-Xmx600m -Xms600m -Xmn200m -XX:MetaspaceSize=100m -XX:MaxMetaspaceSize=200m'

if [ -n "$VMT" ]; then
    if [ $VMT = 'XXL' ]; then
        JVM_P='-Xmx4000m -Xms4000m -Xmn1200m -XX:MetaspaceSize=450m -XX:MaxMetaspaceSize=450m'
    fi
    if [ $VMT = 'XL' ]; then
        JVM_P='-Xmx3000m -Xms3000m -Xmn900m -XX:MetaspaceSize=400m -XX:MaxMetaspaceSize=400m'
    fi
    if [ $VMT = 'X' ]; then
        JVM_P='-Xmx2000m -Xms2000m -Xmn600m -XX:MetaspaceSize=350m -XX:MaxMetaspaceSize=350m'
    fi
    if [ $VMT = 'M2' ]; then
        JVM_P='-Xmx1500m -Xms1500m -Xmn500m -XX:MetaspaceSize=300m -XX:MaxMetaspaceSize=300m'
    fi
    if [ $VMT = 'M' ]; then
        JVM_P='-Xmx1000m -Xms1000m -Xmn330m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=270m'
    fi
    if [ $VMT = 'S' ]; then
        JVM_P='-Xmx600m -Xms600m -Xmn200m -XX:MetaspaceSize=150m -XX:MaxMetaspaceSize=200m'
    fi
fi

# 如果指定了，则使用指定的VM参数
if [ -n "$VMP" ]; then
    JVM_P=${VMP}
fi

JAVA_OPTS=${JAVA_OPTS}" "${JVM_P}

# 其他自定义虚拟机参数
if [ -n "$COP" ]; then
    echo 'COP:'$COP
    JAVA_OPTS=${JAVA_OPTS}" "${COP}
fi

# 覆盖所有虚拟机参数
if [ -n "$FOP" ]; then
    echo 'FOP:'$FOP
    JAVA_OPTS=${FOP}
fi

# 程序其他启动参数设置
if [ -n "$CEP" ]; then
    echo 'CEP:'$CEP
    EP=${EP}" "${CEP}
fi

# 应用jar文件
APP_FILE="/home/app.jar"
if [ ! -f "$APP_FILE" ]; then
    # jar文件 为找到 则退出
    echo "!!!!!!!!!!!!!! /home/app.jar not found !!!!!!!!!!!!"
    exit 0
fi

# 启动命令
START_COMMAND="${JAVA_OPTS} -jar ${APP_FILE} ${EP}"

echo "------------------- env list --------------------"

echo "APP_NAME: ${APP_NAME}"
echo "APP_ENV: ${APP_ENV}"
echo "APP_IP: ${APP_IP}"
echo "JVM options: ${JAVA_OPTS}"
echo "App arguments: ${EP}"
echo "app start command: java ${START_COMMAND}"

echo "------------------- start app -------------------"

# 启动程序
java $START_COMMAND
